package crawler.taobao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import bean.Grass;

import util.MD5;

import database.Dao;

public class Taobao {

	// 链接的超时时间
	private final int timeout = 5000;

	public void begin() {

		try {
			Document doc = Jsoup.connect("http://www.taobao.com")
					.timeout(timeout).get();

			// 需要保存到数据库的链接
			ArrayList<Grass> save = new ArrayList<Grass>();

			// 得到首页的所有链接
			Elements links = doc.getElementsByTag("a");
			// 主页的所有链接，即一级链接
			ArrayList<Grass> firstlist = new ArrayList<Grass>();
			for (Element link : links) {
				Grass l = new Grass();
				String linkHref = link.attr("href");
				l.setHref(linkHref);
				String linkText = link.text();
				l.setText(linkText);
				firstlist.add(l);
			}
			int yy = 0;
			// 通过上面的链接，进入二级页面
			first: for (Grass first : firstlist) {

				// System.out.println(yy++);
				try {
					// 通过一级链接得到二级页面的对象
					doc = Jsoup.connect(first.getHref()).timeout(timeout).get();
				} catch (Exception e) {
					e.printStackTrace();
				}

				// 得到所有的图片
				Elements imgs = doc.getElementsByTag("img");
				HashMap<String, String> map = new HashMap<String, String>();
				for (Element img : imgs) {

					Element parent = img.parent();

					String dkl = img.attr("data-ks-lazyload");
					String src = img.attr("src");
					String imgsrc = null;
					if (dkl != null) {
						imgsrc = dkl;
					}
					if (src != null) {
						imgsrc = src;
					}
					map.put(parent.attr("href"), imgsrc);
				}
				// 得到所有二级页面的链接
				Elements secondlist = doc.getElementsByTag("a");

				int count = 0;
				for (Element second : secondlist) {
					Grass link = new Grass();
					String linkHref = second.attr("href");
					link.setHref(linkHref);
					String linkText = second.text();
					link.setText(linkText);
					if (validate(second)) {
						// 设置图片
						String img = (String) map.get(linkHref);
						link.setImg(img);
						// 设置href的md5值
						link.setMd5(MD5.encode(link.getHref()));
						// 设置价格
						link.setPrice(getPrice(second));
						save.add(link);
						// count++;
						// if(count>5){
						// break first;
						// }
						System.out.println("-------------------------");
						System.out.println(linkText);
						System.out.println(linkHref);
						System.out.println(link.getPrice());
						System.out.println(first.getHref());
						// System.out.println(img);
					}
				}
			}
			System.out.println("抓取完成");
			//Dao dao = new Dao();
			//dao.insert(save);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 判断是否是需要的链接
	public boolean validate(Element e) {
		boolean flag = false;
		String linkHref = e.attr("href");
		String linkText = e.text();
		if (linkHref.indexOf("/item.htm") > 0 && linkText.length() > 6
				&& linkHref.indexOf("on_comment") < 0) {
			flag = true;
		}
		return flag;
	}

	// 得到element中price的方法
	public Double getPrice(Element ele) {
		Double price = null;
		try {
			// 良品价
			Elements list = ele.getElementsByTag("em");
			Element e = list.first();
			price = Double.parseDouble(e.text());

		} catch (Exception e) {
			// e.printStackTrace();
		}
		try {
			Elements parent = ele.parents();
			Elements pricelist = parent.select(".price");
			Element p = pricelist.first();
			price = Double.parseDouble(p.text());
		} catch (Exception e) {

		}
		try {
			Elements parent = ele.parents().parents();
			Elements pricelist = parent.select(".price");
			Element p = pricelist.first();
			price = Double.parseDouble(p.text());
		} catch (Exception e) {

		}
		return price;
	}
}
